<?php
/**
 * Created by mww
 * User: mww
 */

namespace app\admin\controller;
use think\Db;
use think\Request;

class Hospital extends Base
{
    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $where = [];
            if(isset($keyword) && $keyword != ''){
                $where['hos_name'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($hos_sheng) && $hos_sheng != ''){
                $where['hos_sheng'] = $hos_sheng;
            }
            if(isset($hos_shi) && $hos_shi != ''){
                $where['hos_shi'] = $hos_shi;
            }
            if(isset($hos_status) && $hos_status != ''){
                $where['hos_status'] = $hos_status;
            }

            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $field = 'dq.*,u1.district sheng,u2.district shi';
            $count = Db::table('up_hospital')->alias('dq')
                ->join('up_area u1','dq.hos_sheng = u1.district_id','left')
                ->join('up_area u2','dq.hos_shi = u2.district_id','left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_hospital')->alias('dq')
                ->join('up_area u1','dq.hos_sheng = u1.district_id','left')
                ->join('up_area u2','dq.hos_shi = u2.district_id','left')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('hos_status asc,hos_time desc')
                ->field($field)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $province = Db::table('up_area')
            ->field('district_id,district')
            ->where('pid',0)
            ->select();
        $this->assign([
            'province' => $province,
        ]);
        return $this->fetch();
    }
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function quesadd(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'hos_name' => $hos_name,
                'hos_sheng' => $hos_sheng,
                'hos_shi' => $hos_shi,
                'hos_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_hospital')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $province = Db::table('up_area')
            ->field('district_id,district')
            ->where('pid',0)
            ->select();
        $this->assign([
            'province' => $province,
        ]);
        return $this->fetch();
    }
    /**
     * 编辑
     * @return mixed|\think\response\Json
     */
    public function quesedit(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'hos_name' => $hos_name,
                'hos_sheng' => $hos_sheng,
                'hos_shi' => $hos_shi,
                'hos_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_hospital')->where('hos_id', $hos_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $hos_id = input('hos_id');
        $info = Db::table('up_hospital')
            ->where('hos_id', $hos_id)
            ->find();
        $province = Db::table('up_area')
            ->field('district_id,district')
            ->where('pid',0)
            ->select();
        $city = Db::table('up_area')
            ->field('district_id,district')
            ->where('pid',$info['hos_sheng'])
            ->select();
        $this->assign([
            'hos_id' => $hos_id,
            'province' => $province,
            'city' => $city,
            'info' => $info
        ]);
        return $this->fetch();
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function quesdel(){
        $id = input('id');
        $res = Db::table('up_hospital')->where('hos_id',$id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 冻结启用
     * @return mixed|\think\response\Json
     */
    public function changeStatus(){
        $id = input('id');
        $param = [
            'hos_status'=>input('num'),
        ];
        $res = Db::name('hospital')->where('hos_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    /**
     * 导入Excel
     * @return mixed
     */
    public function excelin(){
        set_time_limit(0);
        if(request()->file('file')){
            $file = request()->file('file');
            $info  = $file->getInfo();
            $file_path = $info['tmp_name'];
            //取出文件后缀
            $file_type = explode(".",$info['name']);
            $file_type = end($file_type);
            vendor("PHPExcel.PHPExcel.IOFactory");
            if( $file_type == 'xls' ){
                $inputFileType = 'Excel5';    //这个是读 xls的
            }else{
                $inputFileType = 'Excel2007';//这个是计xlsx的
            }
            $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($file_path);
            $objWorksheet = $objPHPExcel->getActiveSheet();
            $highestRow = $objWorksheet->getHighestRow();
//            print_r($highestRow);die;
            $time = date('Y-m-d H:i:s');
            $data = [];
            for ($i=2;$i<=$highestRow;$i++) {
                $name = $objPHPExcel->getActiveSheet ()->getCell ("A" . $i)->getValue ();//医院
                $sheng = $objPHPExcel->getActiveSheet ()->getCell ("B" . $i)->getValue ();//省
                $shi = $objPHPExcel->getActiveSheet ()->getCell ("C" . $i)->getValue ();//市
                if(trim($name,' ') != '' && trim($sheng,' ') != '' && trim($shi,' ') != ''){
                    $name = trim($name,' ');
                    $sheng = trim($sheng,' ');$sheng = trim($sheng,'市');
                    $shi = trim($shi,' ');
                    $have = Db::table('up_hospital')
                        ->where('hos_name', $name)
                        ->find();
                    if($have){
                        return json(['code' => 100, 'msg' => "{$name}已存在！"]);
                    }else{
                        $sheng_id = Db::table('up_area')
                            ->where('pid', 0)
                            ->where('district', $sheng)
                            ->value('district_id');
                        if(!$sheng_id) return json(['code' => 100, 'msg' => "不存在此省份：{$sheng}！"]);
                        $shi_id = Db::table('up_area')
                            ->where('pid', $sheng_id)
                            ->where('district', $shi)
                            ->value('district_id');
                        if(!$shi_id) return json(['code' => 100, 'msg' => "不存在此省：{$sheng}下的市：{$shi}！"]);
                        $insert = [
                            'hos_name' => $name,
                            'hos_sheng' => $sheng_id,
                            'hos_shi' => $shi_id,
                            'hos_time' => $time,
                        ];
                        $data[] = $insert;
                    }
                }
            }
            if($data){
                $res = Db::table('up_hospital')->insertAll($data);
                if($res){
                    return json(['code' => 200,'msg'=>"导入成功"]);
                }else{
                    return json(['code' => 100,'msg'=>"导入失败"]);
                }
            }else{
                return json(['code' => 100,'msg'=>"有效数据为空"]);
            }
        }
        return $this->fetch();
    }
}